package com.carlyang.studentinfo.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carlyang.studentinfo.dao.ClassDao;
import com.carlyang.studentinfo.dao.StudentDao;
import com.carlyang.studentinfo.model.Class;
import com.carlyang.studentinfo.model.Student;
import com.carlyang.studentinfo.util.DBUtil;
import com.carlyang.studentinfo.util.DateUtil;
import com.carlyang.studentinfo.util.LogUtil;
import com.carlyang.studentinfo.util.ResponseUtil;
import com.carlyang.studentinfo.util.StringUtil;

import net.sf.json.JSONObject;

public class StudentSaveUpdateServlet extends HttpServlet{
	public static final String TAG = "StudentSaveUpdateServlet";
	StudentDao studentDao = new StudentDao();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String stuNo = request.getParameter("stuNo");
		String stuName = request.getParameter("stuName");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String classId = request.getParameter("className");
		//String className = request.getParameter("className");//在Web页中，由于添加信息对话框中的className会传入ID，而在修改数据时传入的className却是班级名
		String email = request.getParameter("email");
		String stuDesc = request.getParameter("stuDesc");
		String id = request.getParameter("id_stu");
		
		LogUtil.i(TAG, "获取参数 ：stuNo:"+stuNo+",stuName:"+stuName+",sex:"+sex+",birthday:"+birthday+",classId:"+classId+",email:"+email+",stuDesc:"+stuDesc+",id:"+id);
		Student student = new Student(stuNo, stuName, sex, DateUtil.formatString(birthday, "yyyy-MM-dd"),Integer.parseInt(classId), email, stuDesc);
		//LogUtil.i(TAG, "传入student:"+student.toString());
		if (StringUtil.isNotEmpty(id)) {//如果获取的id不为空，则设置到student对象中，如果为空，student对象中id默认为0
			student.setId(Integer.parseInt(id));
		}
		LogUtil.i(TAG, student.toString());
		Connection connection=null;
		try {
			LogUtil.i(TAG, "连接数据库......");
			connection = DBUtil.getConnection();
			JSONObject jsonObject = new JSONObject();
			int delNums;
			if (StringUtil.isNotEmpty(id)) {//编辑操作
				delNums = studentDao.updateStudent(connection, student);
			}else {//添加操作
				delNums = studentDao.addStudent(connection, student);				
			}
			if (delNums>0) {
				jsonObject.put("success", "true");
				LogUtil.i(TAG, "保存成功");
			}else {
				jsonObject.put("success", "true");
				jsonObject.put("errorMsg", "保存失败！");
				LogUtil.i(TAG, "保存失败！");
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeCon(connection);
				LogUtil.i(TAG, "关闭数据库连接成功！---------------------------------");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
